#include "stdafx.h"

#include "Input.h"

Input::Input(Ogre::RenderWindow* window)
{
	//TODO refactor to function?
	m_playerAvatar = NULL;
	m_window = window;



    OIS::ParamList pl;
    size_t windowHnd = 0;
    std::ostringstream windowHndStr;

    window->getCustomAttribute("WINDOW", &windowHnd);
    windowHndStr << windowHnd;
    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));

    m_inputManager = OIS::InputManager::createInputSystem( pl );

    m_keyboard = static_cast<OIS::Keyboard*>(m_inputManager->createInputObject( OIS::OISKeyboard, true ));
    m_mouse = static_cast<OIS::Mouse*>(m_inputManager->createInputObject( OIS::OISMouse, true ));

    m_mouse->setEventCallback(this);
    m_keyboard->setEventCallback(this);

    
    windowResized(m_window);
	

	Ogre::WindowEventUtilities::addWindowEventListener(m_window, this);
	

}

Input::~Input(){
	//Remove ourselves as a window listener
	 Ogre::WindowEventUtilities::removeWindowEventListener(m_window, this);
	 windowClosed(m_window);
	 
}
void Input::setPlayerAvatar(PlayerAvatar* avatar)
{
	m_playerAvatar = avatar;

}

bool Input::update(float deltaTime)
{
	//TODO make OgreApplication windowlistener too.
	if(m_window->isClosed())
	{
        return false;
	}
    //Need to capture/update each device
    m_keyboard->capture();
    m_mouse->capture();
 
    if(m_keyboard->isKeyDown(OIS::KC_ESCAPE))
	{
        return false;
	}
	if(m_playerAvatar)
	{
		if(m_keyboard->isKeyDown(OIS::KC_W))
		{
			m_playerAvatar->move(0,0,1);
		}
		if(m_keyboard->isKeyDown(OIS::KC_A))
		{
			m_playerAvatar->move(1,0,0);
		}
		if(m_keyboard->isKeyDown(OIS::KC_S))
		{
			m_playerAvatar->move(0,0,-1);
		}
		if(m_keyboard->isKeyDown(OIS::KC_D))
		{
			m_playerAvatar->move(-1,0,0);
		} 

		
	}
    return true;
	
}

	
bool Input::keyPressed(const OIS::KeyEvent &arg)
{/*
	if(m_playerAvatar)
	{
		switch (arg.key)
		{
		case OIS::KC_W: 
			m_playerAvatar->move(0,0,10);
			break;
		case OIS::KC_A: 
			m_playerAvatar->move(10,0,0);
			break;
		case OIS::KC_S: 
			m_playerAvatar->move(0,0,-10);
			break;
		case OIS::KC_D: 
			m_playerAvatar->move(-10,0,0);
			break;
		default:
			break;
		}
		
	}*/
	return true;
}
bool Input::keyReleased(const OIS::KeyEvent &arg)
{
	return true;
}
	
bool Input::mouseMoved( const OIS::MouseEvent &arg )
{
	
	if(m_playerAvatar)
	{
		m_playerAvatar->turn(-arg.state.X.rel);
		m_playerAvatar->pitchCamera(arg.state.Y.rel);
	}
	return true;
}
bool Input::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
	return true;

}
bool Input::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{

	return true;
}

//Adjust mouse clipping area
void Input::windowResized(Ogre::RenderWindow* rw)
{
    unsigned int width, height, depth;
    int left, top;
    rw->getMetrics(width, height, depth, left, top);

    const OIS::MouseState &ms = m_mouse->getMouseState();
    ms.width = width;
    ms.height = height;
}

//Unattach OIS before window shutdown (very important under Linux)
void Input::windowClosed(Ogre::RenderWindow* rw)
{
    //Only close for window that created OIS
    if( rw == m_window )
    {
        if( m_inputManager )
        {
            m_inputManager->destroyInputObject( m_mouse );
            m_inputManager->destroyInputObject( m_keyboard );

            OIS::InputManager::destroyInputSystem(m_inputManager);
            m_inputManager = 0;
        }
    }
}